package com.chb.friend2;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class RunJob {
	public static void main(String[] args) throws Exception{
		System.setProperty("HADOOP_USER_NAME", "chb");
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(conf);
		Job job = Job.getInstance();
		job.setJobName("Fof");
		job.setJarByClass(RunJob.class);
		//Mapper
		job.setMapperClass(FofMapper.class);
		job.setMapOutputKeyClass(Fof.class);
		job.setMapOutputValueClass(IntWritable.class);
		job.setJar("C:\\Users\\12285\\Desktop\\Fof.jar");
		//Reducer
		job.setReducerClass(FofReducer.class);
		job.setInputFormatClass(KeyValueTextInputFormat.class);
		//输入
		FileInputFormat.addInputPaths(job, "/user/chb/input/friend");
		//输出
		Path out = new Path("/user/chb/output/fof1");
		if (fs.exists(out)) {
			fs.delete(out, true);
		}
		FileOutputFormat.setOutputPath(job, out);
		boolean f = job.waitForCompletion(true);
		if (f) {//第一个mapper执行完成，开始执行第二个mapper
			run2();
		}
	}

	public static void run2() throws Exception {
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(conf);
		Job job = Job.getInstance();
		job.setJobName("Fof");
		job.setJarByClass(RunJob.class);
		//Mapper
		job.setMapperClass(SortMapper.class);
		job.setMapOutputKeyClass(User.class);
		job.setMapOutputValueClass(User.class);
		job.setJar("C:\\Users\\12285\\Desktop\\Fof.jar");
		//Reducer
		job.setReducerClass(SortReducer.class);
		job.setInputFormatClass(KeyValueTextInputFormat.class);
		//设置分组
		job.setGroupingComparatorClass(Fof2Group.class);
		//设置排序
		job.setSortComparatorClass(Fof2Sort.class);
		//输入
		FileInputFormat.addInputPaths(job, "/user/chb/output/fof");
		//输出
		Path out = new Path("/user/chb/output/fof1");
		if (fs.exists(out)) {
			fs.delete(out, true);
		}
		FileOutputFormat.setOutputPath(job, out);
		
		if (job.waitForCompletion(true)) {
			System.out.println("任务完成。。。");
		}
	}
}
